#include "dpLibrary\dpWindows.h"
#include "winlib.h"
#include "AFObjectFactory.h"
#include "AFGraphics.h"
#include "AFEnemy.h"

//initializes a path object
void initPath(motionPath* path, int x, int y)
{
  Node *node1 = (Node*)malloc(sizeof(Node));  //malloc?
  path->numNodes = 1;
  node1->X = x;
  node1->Y = y;
  node1->next = 0;
  path->myNode = node1;
  path->head = node1;
}

//adds nodes to the list in the path object
void addNode(motionPath* path, int x, int y)
{
  Node* node = (Node*)malloc(sizeof(Node));   //malloc?
  path->numNodes++;
  node->X = x;
  node->Y = y;
  node->next = path->head;
  path->myNode->next = node;
  path->myNode = node;
}


int moveEnemy(Sprite* enemy, motionPath* path1)
{
  //get direction differences
  int xdir, ydir;
  //make the list loop
  
  xdir = (path1->myNode->next->X - enemy->world.X);
  ydir = (path1->myNode->next->Y - enemy->world.Y);
  //the difference is vertical
  if(xdir == 0 && ydir == 0)
  {
    //move to next node
    path1->myNode = path1->myNode->next;
    return 0;  //did not move enemy
  }
  if(xdir == 0)
  {
    //the next node is below
    if(enemy->world.Y < path1->myNode->next->Y)
    {
      enemy->world.Y++;
    }
    //the next node is above
    if(enemy->world.Y > path1->myNode->next->Y)
    {
      enemy->world.Y--;
    }
    return 1;  //moved enemy
  }
  //the difference is horizontal
  if(ydir == 0)
  {
    //the next node is to the right
    if(enemy->world.X < path1->myNode->next->X)
    {
      enemy->world.X++;
    }
    //the next node is to the left
    if(enemy->world.X > path1->myNode->next->X)
    {
      enemy->world.X--;
    }
    return 1; //moved enemy
  }
  return 0;
}
/*
void enemyMotion()
{
  int playerControlled = 0;
  Sprite* enemy = CreateSprite(RED, 0, 0, 0, 0, 10,10);
  motionPath* path1 = (motionPath*)malloc(sizeof(motionPath));

  //init path object with the first node in the path
  initPath(path1, 5, 5);
  addNode(path1, 5, 10);
  addNode(path1, 10, 10);
  addNode(path1, 10, 5);

  //start the enemy at the first path node
  enemy->world.X = path1->head->X;
  enemy->world.Y = path1->head->Y;
  
  if(!playerControlled)
  {
    moveEnemy(enemy, path1);
  }
}*/